<?php


namespace app\utils;

class Response
{
    public static function success($data = [], $msg = "成功")
    {
        self::toJson(200, $msg, $data);
    }

    public static function fail($msg = "失败", $code = 500)
    {
        self::toJson($code, $msg, []);
    }

    private static function toJson(int $code, $msg = "", $datas = [])
    {
        $jsonData = [
            'code' => $code,
            'msg' => $msg
        ];
        if ($code == 200) {
            $jsonData['data'] = $datas;
        }
        echo json_encode($jsonData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        die;
    }
}